<template>
<div>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="onRoutes"
            :collapse="collapse"
            text-color="#FFFFFF"
            active-text-color="#3A4B5A"
            :background-color="switchHeadTab == '中药饮片'?'#3566EE':'#6C7D92'"
            unique-opened
            router
        >
            <template v-for="item in itemlist">
                <template v-if="item.subs">
                    <el-submenu :index="item.index" :key="item.index">
                        <template slot="title">
                            <i :class="item.icon"></i>
                            <span style="color:#fff" slot="title">{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.subs">
                            <el-submenu
                                v-if="subItem.subs"
                                :index="subItem.index"
                                :key="subItem.index"
                            >
                                <template slot="title">
                                    {{ subItem.title }}
                                </template>
                                <el-menu-item
                                    :class="'/'+$route.path.split('/')[1]==subItem.index?'is-active':''"
                                    v-for="(threeItem,i) in subItem.subs"
                                    :key="i"
                                    :index="threeItem.index"
                                >{{ threeItem.title }}</el-menu-item>
                            </el-submenu>
                            <el-menu-item
                                :class="'/'+$route.path.split('/')[1]==subItem.index?'is-active':''"
                                v-else
                                :index="subItem.index"
                                :key="subItem.index"
                            >{{ subItem.title }}</el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item  :index="item.index" :key="item.index" :class="'/'+$route.path.split('/')[1]==item.index?'is-active':''">
                        <i :class="item.icon"></i>
                        <span style="color:#fff" slot="title">{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</div>

</template>

<script>
/**  兄弟快跑，我不知道我是第几任前端开发了，但是这个项目真的要老命，屎山一大堆一大堆的，逻辑极其混乱，耦合度高，我反正不敢重构，快跑吧！ 2024.5.24 sameki **/
import bus from '../common/bus';
import { mapState } from 'vuex';
import { inject } from 'vue';
export default {
    inject: ['reload'],
    data() {
        return {
            itemlist:[],
            collapse: false,
            items: [],
            items1:[
                // {
                //     icon: 'iconfont icon12',
                //     index: '/systemmanagecome',
                //     title: '上市备案号'
                // },
    //             {
    //                 icon: 'iconfont iconIconxingzhuang21',
    //                 index: '/detectionMechanism',
    //                 title: '检测机构'
    //             },
				// {
				//     icon: 'iconfont iconIconxingzhuang3',
				//     index: '/printTemplate',
				//     title: '打印模板'
				// }
            ],
            items2:[],
            data:[]
        };
    },
    watch:{
        switchFlag(n,o){
            if(n == '/home'){
            }
        },
    },
    computed: {
        ...mapState(['switchHeadTab']),
        switchFlag(){
            return this.$route.path
        },
        onRoutes() {
            return this.$route.path.replace('/', '');
        }
    },
    created() {
        this.data = this.$store.getters.permission
        // console.log(this.data)
        var userType = localStorage.getItem("userType")
        if(userType == 1){
            for(let s = 0;s<this.data.length;s++){
                if(this.data[s].name == "信息与权限管理"){
                    this.items.push({
                        icon: 'iconfont icon41',
                        index: '/factory',
                        title: '信息与权限管理',
                        subs:[]
                    })
                    // this.$store.commit("setjc",this.data[i].children)
                }

                if(this.data[s].name == "企业信息"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="信息与权限管理"){
                                _this.items[k].subs.push({
                                    index: '/factory',
                                    title: '企业信息',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                 if(this.data[s].name == "数据授权"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="信息与权限管理"){
                                _this.items[k].subs.push({
                                    index: '/informationfactory',
                                    title: '数据授权',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                if(this.data[s].name == "员工管理"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="信息与权限管理"){
                                _this.items[k].subs.push({
                                    index: '/managestaff',
                                    title: '员工管理',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                 if(this.data[s].name == "权限配置"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="信息与权限管理"){
                                _this.items[k].subs.push({
                                    index: '/managepermission',
                                    title: '权限配置',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }

                if(this.data[s].name == "第三方管理"){
                    this.items.push({
                        icon: 'iconfont icon41',
                        index: '/managesupplierManagement',
                        title: '第三方管理',
                        subs:[]
                    })
                    // this.$store.commit("setjc",this.data[i].children)
                }
                if(this.data[s].name == "供应商管理"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="第三方管理"){
                                _this.items[k].subs.push({
                                    index: '/managesupplierManagement',
                                    title: '供应商管理',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                 if(this.data[s].name == "客户管理"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="第三方管理"){
                                _this.items[k].subs.push({
                                    index: '/managecustomerManagement',
                                    title: '客户管理',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }

                if(this.data[s].name == "备案号管理"){
                    this.items.push({
                        icon: 'iconfont icon41',
                        index: '/managerecord',
                        title: '备案号管理',
                        subs:[]
                    })
                    // this.$store.commit("setjc",this.data[i].children)
                }
                if(this.data[s].name == "销售备案号"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="备案号管理"){
                                _this.items[k].subs.push({
                                    index: '/managerecord',
                                    title: '销售备案号',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                if(this.data[s].name == "系统设置"){
                    this.items.push({
                        icon: 'iconfont icon41',
                        index: '/managematerialsManagement',
                        title: '系统设置',
                        subs:[]
                    })
                    // this.$store.commit("setjc",this.data[i].children)
                }
                if(this.data[s].name == "物料管理"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="系统设置"){
                                _this.items[k].subs.push({
                                    index: '/managematerialsManagement',
                                    title: '物料管理',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                if(this.data[s].name == "检测依据"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="系统设置"){
                                _this.items[k].subs.push({
                                    index: '/managecheckingBasis',
                                    title: '检测依据',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                if(this.data[s].name == "检测机构"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="系统设置"){
                                _this.items[k].subs.push({
                                    index: '/managedetectionMechanism',
                                    title: '检测机构',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                if(this.data[s].name == "工艺工序"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="系统设置"){
                                _this.items[k].subs.push({
                                    index: '/manageprocess',
                                    title: '工艺工序',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                if(this.data[s].name == "产品信息"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="系统设置"){
                                _this.items[k].subs.push({
                                    index: '/manageproductMessage',
                                    title: '产品信息',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                if(this.data[s].name == "单位管理"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="系统设置"){
                                _this.items[k].subs.push({
                                    index: '/manageunit',
                                    title: '单位管理',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                if(this.data[s].name == "打印模板"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="系统设置"){
                                _this.items[k].subs.push({
                                    index: '/manageprintTemplate',
                                    title: '打印模板',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }
                if(this.data[s].name == "系统日志"){
                    var _this = this
                    setTimeout(function(){
                        for(var k=0;k<_this.items.length;k++){
                            if(_this.items[k].title=="系统设置"){
                                _this.items[k].subs.push({
                                    index: '/managesystem',
                                    title: '系统日志',
                                })
                            }
                        }
                    },1000)
                    // _this.$store.commit("setkl",this.data[i].children)
                }

            }
        }else{
            if(this.data){
                for (let i = 0; i < this.data.length; i++) {
                    if(this.data[i].name == "原料入库"){
                        this.items.push({
                            icon: 'iconfont icon81',
                            index: '/materialCheck',
                            title: '原料入库'
                        })
                        this.$store.commit("setyl",this.data[i].children)
                    }
                    if(this.data[i].name == "库存管理"){
                        this.items.push({
                            icon: 'iconfont icon2',
                            index: '/inventory',
                            title: '库存管理'
                        })
                        this.$store.commit("setkc",this.data[i].children)
                    }
                    if(this.data[i].name == "生产管理"){
                        this.items.push({
                            icon: 'iconfont icon3',
                            index: '/production',
                            title: '生产管理'
                        })
                        this.$store.commit("setsc",this.data[i].children)
                    }
                    if(this.data[i].name == "检测管理"){
                        this.items.push({
                            icon: 'iconfont icon1',
                            index: '/detection',
                            title: '检测管理'
                        })
                        this.$store.commit("setjc",this.data[i].children)
                    }
                    if(this.data[i].name == "分装赋码"){
                        this.items.push({
                            icon: 'iconfont icon41',
                            index: '/subpackage',
                            title: '分装赋码',
                            subs:[]
                        })
                        // this.$store.commit("setjc",this.data[i].children)
                    }
                    if(this.data[i].name == '外包管理'){
                        console.log(this.data[i]);
                    }
                    if(this.data[i].name == "外包管理"){
                        this.items.push({
                            icon: 'iconfont iconic-baseline-view-compact',
                            index: '/epiboly',
                            title: '外包管理'
                        })
                        this.$store.commit("setwb",this.data[i].children)
                    }
                    if(this.data[i].name == "产成品入库单"){
                        this.items.push({
                            icon: 'iconfont iconic-baseline-view-compact',
                            index: '/productStorageList',
                            title: '产成品入库单'
                        })
                    }
                    if(this.data[i].name == "订单管理"){
                        this.items.push({
                            icon: 'iconfont iconic-baseline-view-list',
                            index: '/indent',
                            title: '订单管理',
                            subs:[]
                        })
                        // this.$store.commit("set",this.data[i].children)
                    }


                    if(this.data[i].name == "数据发送"){
                        var _this = this
                        setTimeout(function(){
                            for(var k=0;k<_this.items.length;k++){
                                if(_this.items[k].title=="订单管理"){
                                    _this.items[k].subs.push({
                                        index: '/indent',
                                        title: '数据发送',
                                    })
                                }
                            }
                        },1000)
                        _this.$store.commit("setdata",this.data[i].children)
                    }
                    if(this.data[i].name == "供货管理"){
                        var _this = this
                        setTimeout(function(){
                            for(var k=0;k<_this.items.length;k++){
                                if(_this.items[k].title=="订单管理"){
                                    _this.items[k].subs.push({
                                        index: '/supply',
                                        title: '供货管理',
                                    })
                                }
                            }
                        },1000)
                        _this.$store.commit("setsupply",this.data[i].children)
                    }
                    if(this.data[i].name == "召回管理"){
                        this.items.push({
                            icon: 'iconfont icon71',
                            index: '/recall',
                            title: '召回管理'
                        })
                        this.$store.commit("setzh",this.data[i].children)
                    }
                    if(this.data[i].name == "统计数据"){
                        this.items.push({
                            icon: 'iconfont icon61',
                            index: '/statistics',
                            title: '统计数据'
                        })
                        this.$store.commit("settj",this.data[i].children)
                    }
                    if(this.data[i].name == "追溯查询"){
                        this.items.push({
                            icon: 'iconfont iconsousuo',
                            index: '/traceBack',
                            title: '追溯查询'
                        })
                        this.$store.commit("settj",this.data[i].children)
                    }
                    if(this.data[i].name == "包装指令"){
                        var _this = this
                        setTimeout(function(){
                            for(var k=0;k<_this.items.length;k++){
                                if(_this.items[k].title=="分装赋码"){
                                    _this.items[k].subs.push({
                                        index: '/packing',
                                        title: '包装指令',
                                    })
                                }
                            }
                        },1000)
                        _this.$store.commit("setkl",this.data[i].children)
                    }
                    if(this.data[i].name == "审核管理"){
                        var _this = this
                        setTimeout(function(){
                            for(var k=0;k<_this.items.length;k++){
                                if(_this.items[k].title=="分装赋码"){
                                    _this.items[k].subs.push({
                                        index: '/subpackage',
                                        title: '审核管理',
                                    })
                                }

                            }
                        },1000)
                        _this.$store.commit("setfb",this.data[i].children)
                    }
                    // if(this.data[i].name == "审核管理"){
                    //     var _this = this
                    //     setTimeout(function(){
                    //         for(var k=0;k<_this.items.length;k++){
                    //             if(_this.items[k].title=="分装赋码"){
                    //                 _this.items[k].subs.push({
                    //                     index: '/examine',
                    //                     title: '审核管理',
                    //                 })
                    //             }

                    //         }
                    //     },1000)
                    //      _this.$store.commit("setsh",this.data[i].children)
                    // }
                    if(this.data[i].name == "打印管理"){
                        var _this = this
                        setTimeout(function(){
                            for(var k=0;k<_this.items.length;k++){
                                if(_this.items[k].title=="分装赋码"){
                                    _this.items[k].subs.push({
                                        index: '/printTask',
                                        title: '打印管理',
                                    })
                                }

                            }
                        },1000)
                        _this.$store.commit("setdy",this.data[i].children)
                    }
                    if(this.data[i].name == "企业信息" && this.switchHeadTab == '生产系统'){
                        this.items1.push({
                            icon: 'iconfont iconIconxingzhuang',
                            index: '/business',
                            title: '企业信息'
                        })
                        this.$store.commit("setqy",this.data[i].children)
                    }
                    if(this.data[i].name == "员工管理" && this.switchHeadTab == '生产系统'){
                        this.items1.push({
                            icon: 'iconfont iconIconxingzhuang2',
                            index: '/staffsManagement',
                            title: '员工管理'
                        })
                        this.$store.commit("setyg",this.data[i].children)
                    }
                    if(this.data[i].name == "权限配置" && this.switchHeadTab == '生产系统'){
                        this.items1.push({
                            icon: 'iconfont iconIconlujing3',
                            index: '/permission',
                            title: '权限配置'
                        })
                        this.$store.commit("setqx",this.data[i].children)
                    }
                    if(this.data[i].name == "供应商管理"){
                        this.items1.push({
                            icon: 'iconfont icon4',
                            index: '/supplierManagement',
                            title: '供应商管理'
                        })
                        this.$store.commit("setgys",this.data[i].children)
                    }
                    if(this.data[i].name == "客户管理"){
                        this.items1.push({
                            icon: 'iconfont icon5',
                            index: '/customerManagement',
                            title: '客户管理'
                        })
                        this.$store.commit("setkh",this.data[i].children)
                    }
                    if(this.data[i].name == "物料管理"){
                        this.items1.push({
                            icon: 'iconfont icon6',
                            index: '/materialsManagement',
                            title: '物料管理'
                        })
                        this.$store.commit("setwl",this.data[i].children)
                    }
                    if(this.data[i].name == "检测依据"){
                        this.items1.push({
                            icon: 'iconfont icon7',
                            index: '/checkingBasis',
                            title: '检测依据'
                        })
                        this.$store.commit("setjcyj",this.data[i].children)
                    }
                    if(this.data[i].name == "检测机构"){
                        this.items1.push({
                            icon: 'iconfont iconIconxingzhuang21',
                            index: '/detectionMechanism',
                            title: '检测机构',
                        })
                        this.$store.commit("setjcjg",this.data[i].children)
                    }
                    if(this.data[i].name == "工艺工序"){
                        this.items1.push({
                            icon: 'iconfont icon8',
                            index: '/process',
                            title: '工艺工序'
                        })
                        this.$store.commit("setgygx",this.data[i].children)
                    }
                    if(this.data[i].name == "产品信息"){
                        this.items1.push({
                            icon: 'iconfont icon9',
                            index: '/productMessage',
                            title: '产品信息'
                        })
                        this.$store.commit("setcpxx",this.data[i].children)
                    }
                    if(this.data[i].name == "生产许可证" && this.switchHeadTab == '中药饮片'){
                        this.items1.push({
                            icon: 'iconfont icon9',
                            index: '/productLicence',
                            title: '生产许可证'
                        })
                        this.$store.commit("setProductLicense",this.data[i].children)
                    }
                    if(this.data[i].name == "单位管理"){
                        this.items1.push({
                            icon: 'iconfont icon10',
                            index: '/unit',
                            title: '单位管理'
                        })
                        this.$store.commit("setdw",this.data[i].children)
                    }
                    if(this.data[i].name == "销售备案号" && this.switchHeadTab == '生产系统'){
                        this.items1.push({
                            icon: 'iconfont iconIconxingzhuang3',
                            index: '/systemmanagerecord',
                            title: '销售备案号'
                        })
                        // this.$store.commit("setdymb",this.data[i].children)
                    }
                    if(this.data[i].name == "上市备案号" && this.switchHeadTab == '生产系统'){
                        this.items1.push({
                            icon: 'iconfont iconIconxingzhuang3',
                            index: '/systemmanagecome',
                            title: '上市备案号'
                        })
                        // this.$store.commit("setdymb",this.data[i].children)
                    }
                    if(this.data[i].name == "打印模板(国标)" && this.switchHeadTab == '生产系统'){
                        this.items1.push({
                            icon: 'iconfont iconIconxingzhuang3',
                            index: '/printTemplatenew',
                            title: '打印模板(国标)'
                        })
                        // this.$store.commit("setdymb",this.data[i].children)
                    }
                    if(this.data[i].name == "打印模板(省标)" && this.switchHeadTab == '生产系统'){
                        this.items1.push({
                            icon: 'iconfont iconIconxingzhuang3',
                            index: '/printTemplate',
                            title: '打印模板(省标)'
                        })
                        this.$store.commit("setdymb",this.data[i].children)
                    }
                    if(this.data[i].name == "打印模板" && this.switchHeadTab == '中药饮片'){
                        this.items1.push({
                            icon: 'iconfont iconIconxingzhuang3',
                            index: '/printTemplateMedicine',
                            title: '打印模板'
                        })
                        // this.$store.commit("setdymb",this.data[i].children)
                    }
                    if(this.data[i].name == "系统日志"){
                        this.items1.push({
                            icon: 'iconfont iconIconxingzhuang1',
                            index: '/system',
                            title: '系统日志'
                        })
                        // console.log(this.items2)
                        // this.$store.commit("setgrsz",this.data[i].children)
                    }
                    if(this.data[i].name == "个人设置"){
                        this.items2.push({
                            icon: 'iconfont icon11',
                            index: '/personage',
                            title: '个人设置'
                        })
                        this.$store.commit("setgrsz",this.data[i].children)
                    }
                    // console.log(this.data[i].name == "系统日志")


                }

            }
        }


        // console.log(this.data)
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', msg => {
            this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });

    },
    mounted(){
        // console.log(this.items1)
     this.$store.commit("settzxtsz",this.items1)
        var ide = localStorage.getItem("ide")
        if(ide == "qwe"){
            this.itemlist = this.items1
        }else{
            if(ide == "qqq"){
                this.itemlist = this.items
            }else{
                this.itemlist = this.items2
            }

        }
    },

};
</script>

<style scoped lang="less">
i{
    display: inline-block;
    width:30px;
    text-align: center;
}
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
    background-color: #6C7D92;
}
.sidebar::-webkit-scrollbar {
    width: 64px;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
    background: #6C7D92;
}
.sidebar > ul {
    height: 100%;
}

.el-submenu .el-menu-item{
    padding-left: 52px !important;
}
/* 设置鼠标悬停时sidebar-el-menu的样式 */
//.el-tooltip__popper.is-dark{
//    span{
//        color: #409EFF !important;
//    }
//}
//
//.sidebar-el-menu .el-menu-item:hover{
//    background-color: #6C7D92 !important;
//}
//.sidebar-el-menu .el-menu-item:hover span{
//    color: #ffffff !important;
//}
//.sidebar-el-menu .el-menu-item:hover i{
//    color: #ffffff !important;
//}
.sidebar-el-menu .el-menu-item.is-active{
    color: #409EFF !important;
    background: #3A4B5A !important;
    span{
        color: #409EFF !important;
    }
    i{
        color: #409EFF !important;
    }
}
.el-submenu__title .iconfont{
    color: #FFFFFF;
    margin: 0 5px 0 3px;
    width: 24px;
    text-align: center;
    font-size: 18px;
    vertical-align: middle
}
.el-menu-item i{
    color:#FFFFFF;
}
</style>
